const app = require("../app")
const {
  USERNAME_OR_PASSWORD_IS_REQUIRED,
  USERNAME_IS_ALREADY_EXISTS,
  USERNAME_IS_NOT_EXISTS,
  PASSWORD_IS_INCORRECT,
  NOT_TOKEN,
  UNAUTHORIZATION,
  PARAMS_INCORRECT,
  OPERATION_IS_NOT_ALLOWED,
} = require("../config/error-constants")

app.on("error", (error, ctx) => {
  let code = 0
  let message = "未知错误"
  switch (error) {
    case USERNAME_OR_PASSWORD_IS_REQUIRED:
      code = -1001
      message = "用户名或者密码不能为空~"
      break
    case USERNAME_IS_ALREADY_EXISTS:
      code = -1002
      message = "用户名已经存在~"
      break
    case USERNAME_IS_NOT_EXISTS:
      code = -1003
      message = "用户名不存在~"
      break
    case PASSWORD_IS_INCORRECT:
      code = -1004
      message = "密码不正确~"
      break
    case NOT_TOKEN:
      code = -1005
      message = "token未携带~"
      break
    case UNAUTHORIZATION:
      code = -1006
      message = "token无效或者token已经过期~"
      break
    case PARAMS_INCORRECT:
      code = -1007
      message = `参数错误,请检查参数${ctx.errorParams}是否正确~`
      break
    case OPERATION_IS_NOT_ALLOWED:
      code = -1008
      message = `权限不足或者动态不存在~`
      break
  }
  ctx.body = {
    code,
    message,
  }
})
